package io.g740.util;

import java.net.URI;

/**
 * 解析URL
 *
 * @author YunKaiLuo
 * @date 2024/10/22
 */
public class UrlUtil {
    public static String parserMySqlUri(
            String urlString
    ) {

        try {
            URI uri = new URI(urlString);
            if ("jdbc".equals(uri.getScheme())) {
                URI subUri = new URI(uri.getRawSchemeSpecificPart());
                return subUri.getHost() + ":" + subUri.getPort();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 检查mysql的主机地址和端口是否相等
     *
     * @param sourceUri
     * @param targetUri
     * @return
     */
    public static boolean checkIfTheHostAndPortOfMysqlAreEqual(
            String sourceUri,
            String targetUri
    ) {
        return parserMySqlUri(sourceUri).equals(parserMySqlUri(targetUri));
    }
}
